/*
 * @Author: zhenghao01
 * @Date:   2021-08-18 16:59:51
 * @Last Modified by:   zhenghao01
 * @Last Modified time: 2021-08-19 17:48:08
 */
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
    mode: 'production',
    entry:'./src/index.js',
    resolve: {
        extensions: ['.js', '.scss'],
        alias: {
            style: path.resolve(__dirname, './src/style/')
        }
    },
    output: {
        filename: 'js/[name]-bundle.js',
        path: path.resolve(__dirname, 'dist'),
        environment: {
            arrowFunction: false,
            const: false
        }
    },
    module: {
        rules: [{
                test: /\.js$/,
                include: path.resolve(__dirname, 'src'),
                use: 'babel-loader'
            },
            {
                test: /\.scss$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style/[name].css'
        }),
        new HtmlWebpackPlugin({
            template: './public/index.html',
            title: '插件的使用',
            inject: 'body',
            hash: true
        })
    ],
    optimization: {
        splitChunks: {
            chunks: 'all',
            automaticNameDelimiter: '-',
            cacheGroups: {
                vendors: {
                    name: `vendors`,
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10,
                    chunks: 'initial'
                },
                 common: {
                    name: `common`,
                    minChunks: 2,
                    priority: -20,
                    chunks: 'initial',
                    reuseExistingChunk: true
                }
            }
        }
    }
}